﻿<template id="@(ViewBag.GetCompTemplate("form"))">
    <el-dialog v-model="visible" :width="500" :title="title" :destroy-on-close="true" :close-on-click-modal="false" :close-on-press-escape="false" draggable v-on:close="closed">
        <el-divider class="layout-page-form-top-border"></el-divider>
        <el-form ref="formRef" :rules="rules" :disabled="disabled" :model="record" :hide-required-asterisk="disabled" label-width="auto" label-suffix=" :">
            <el-form-item label="角色名称" prop="Title">
                <el-input v-model="record.Title"></el-input>
            </el-form-item>
            <el-form-item label="数据权限" prop="DataScope">
                <el-select v-model="record.DataScope" placeholder="请选择">
                    <el-option value="本人">本人</el-option>
                    <el-option value="所有组织">所有组织</el-option>
                    <el-option value="仅所属组织">仅所属组织</el-option>
                    <el-option value="所属组织及以下">所属组织及以下</el-option>
                    <el-option value="指定组织">指定组织</el-option>
                </el-select>
            </el-form-item>
            <el-form-item v-if="record.DataScope=='指定组织'" label="指定组织" prop="OrgIdList">
                <el-tree-select v-model="record.OrgIdList" filterable multiple clearable node-key="Id" check-strictly placeholder="请选择指定组织，支持多选模糊搜索" :data="orgTreeData" :props="{value:'Id',label:'Title'}"></el-tree-select>
            </el-form-item>
            <el-form-item label="角色介绍" prop="Remark">
                <el-input v-model="record.Remark" type="textarea"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="Sequence">
                <el-slider v-model="record.Sequence" show-input :min="1"></el-slider>
            </el-form-item>
            <el-form-item label="状态" prop="Status">
                <el-radio-group v-model="record.Status">
                    <el-radio-button value="ok" label="启用"></el-radio-button>
                    <el-radio-button value="no" label="停用"></el-radio-button>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button v-on:click="close">取消</el-button>
            <el-button type="primary" v-on:click="submit">确定</el-button>
        </template>
    </el-dialog>
</template>
<script type="text/javascript">
    var @(ViewBag.GetCompName("form")) = {
        template: "#@(ViewBag.GetCompTemplate("form"))", data() {
            return {
                path: "@(ViewBag.PagePath)",
                visible: false,
                title: null,
                rules: {
                    Title: [{ required: true, message: "请填写角色名称" }]
                },
                record: {},
                orgTreeData: [],
                disabled: false
            }
        },
        methods: {
            formInit(record){
                var that = this;
                app.func.formInit([
                    (loading) => {
                        app.func.getData(`/sys/org/getdata`, null, null, result => {
                            var filter = record && record.Id ? (m, pid) => {
                                return m.Pid == pid && m.Id != record.Id;
                            } : (m, pid) => { return m.Pid == pid; };
                            var treeData = app.func.structureFilterTree(result.data.rows, 0, filter);
                            that.orgTreeData = treeData;
                            loading.close();
                        });
                    }
                ]);
            },
            reset(){
                this.record.Status = "ok";
                this.record.Sequence = 99;
                this.record.OrgIdList = [];
            },
            open(text, record){
                var that = this;
                that.title = text;
                if (record && record.Id) {
                    app.func.getEntity(`${that.path}/get`, record.Id, result => {
                        that.record = result.data;
                    });
                }
                else { that.reset(); }
                that.visible = true;
                that.formInit(record);
            },
            close(){
                this.visible = false;
            },
            closed(){
                this.title = null;
                this.record = {};
            },
            submit(){
                var that = this;
                that.$refs.formRef.validate(valid => {
                    if (valid) {
                        app.func.submit(`${that.path}/save`, that.record, result => {
                            that.close(); that.$emit("refresh");
                        });
                    }
                });
            }
        },
        mounted() { }
    };
</script>